import { EVENT_CODE } from '@vc/constants';

// 手机号格式化加空格
export const phoneAddBlank = (value) => {
  let showValue = value.replace(/\D/g, '').trim();
  if (showValue.length > 11) {
    showValue = showValue.substr(0, 11);
  }
  let renderValue = '';

  for (let i = 0; i < showValue.length; i++) {
    renderValue += showValue[i];
    if (i === 2 || i === 6) {
      renderValue += ' ';
    }
  }
  return renderValue;
};

// 输入框去除空格
export const formatPhoneBlack = (e) => {
  const {
    target: { value },
    code
  } = e;
  let renderValue = phoneAddBlank(value);
  const renderLen = renderValue.length;
  if (code === EVENT_CODE.backspace && renderValue[renderLen - 1] === ' ') {
    renderValue = renderValue.substr(0, renderLen - 1);
  }
  return renderValue;
};
